import 'package:flutter/material.dart';
import 'package:mine_sweeper/screens/connection.dart';
import 'package:mine_sweeper/screens/mine_sweeper_app.dart';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final difficulty = ["简单", "普通", "困难"];
  String value = "普通";

  int get amntOfBombs {
    if (value == "普通") {
      return 40;
    } else if (value == "简单") {
      return 20;
    } else {
      return 70;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(255, 255, 255, 255),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/bg.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              "快来找兔子",
              style: TextStyle(
                fontSize: 56,
                fontWeight: FontWeight.bold,
                color: Color.fromARGB(255, 235, 189, 4),
              ),
            ),
            SizedBox(height: 40),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  "选择本局难度:",
                  style: TextStyle(fontSize: 24, fontWeight: FontWeight.w100),
                ),
                SizedBox(width: 10),
                DropdownButton<String>(
                  value: value,
                  items: difficulty.map(buildMenuItem).toList(),
                  onChanged: (value) => setState(
                      () => value == null ? value == null : this.value = value),
                ),
              ],
            ),
            SizedBox(height: 20),
            InkWell(
              onTap: () {
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => MineSweeperApp(amntOfBombs)));
              },
              child: Container(
                width: 170,
                height: 50,
                decoration: BoxDecoration(
                  color: Color.fromARGB(255, 237, 179, 4),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Center(
                  child: Text(
                    '开始游戏',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(height: 50),
            TextButton(
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => Connection()));
              },
              child: Text("点击查看积分榜"),
              style: TextButton.styleFrom(primary: Colors.green[900]),
            ),
          ],
        ),
      ),
    );
  }

  DropdownMenuItem<String> buildMenuItem(String item) => DropdownMenuItem(
        value: item,
        child: Text(
          item,
          style: item == "普通"
              ? TextStyle(
                  color: Color.fromARGB(255, 250, 179, 25),
                  fontWeight: FontWeight.bold)
              : item == "困难"
                  ? TextStyle(color: Colors.red, fontWeight: FontWeight.bold)
                  : TextStyle(
                      color: Colors.green[900], fontWeight: FontWeight.bold),
        ),
      );
}
